<?php
class Class_Cart{
    public static function cartArrayToStr($Array_Cart)
    {
        $ret="";
        foreach ($Array_Cart as $Key=>$value)
        {
            if($ret=="")
            {
                $ret=$Key."-".$value;
            }
            else
            {
                $ret.="|".$Key."-".$value;
            }
        }
        return $ret;
    }
    
    public static function cartStrToArray($str_Cart)
    {
        $line=explode("|",$str_Cart);

        $cartinfo=array();
        foreach ($line as $value)
        {
            if($value!="")
            {
                $temp=explode('-',$value);
                $id=(string)$temp[0];
                if(!array_key_exists($id, $cartinfo))
                {
                    $cartinfo[$id]=0;
                }
                $cartinfo[$id]+=(int)$temp[1];
            }
        }
        return $cartinfo;
    }
    
    public static function getCart($Userid = "")
    {
        if($Userid=="")
        { return isset($_SESSION['shopjpcart'])?$_SESSION['shopjpcart']:"";}
        
        require_once( "../system/Class_M_DAO.php" );$M_dao = new Class_M_DAO;
        $SQL="SELECT `cartValue` FROM `d_cart` WHERE `user_id`=".$Userid;
        $data = $M_dao->get_rows($SQL);
        if(count($data)>0)
        {
            return $data[0]["cartValue"];
        }
        else
        {
            return "";
        }
    }
    
    
    public static function setLoginCart($Userid){
    	$session_carts  =  Class_Cart::getCart("");
     	$user_carts     =   Class_Cart::getCart($Userid);
     	
     	$session_carts_arr =  Class_Cart::cartStrToArray($session_carts);
     	$user_carts_arr    =  Class_Cart::cartStrToArray($user_carts);
     	$newArr =$user_carts_arr+  $session_carts_arr ;//two array + merge
     	
     	$str_cart =  Class_Cart::cartArrayToStr( $newArr );
     	
     	unset($_SESSION['shopjpcart']);
     	 Class_Cart::setCart($Userid ,$str_cart);
     	
     }
    
    
    public static function setCart($Userid ,$str_Cart)
    {
        if($Userid=="")
        {$_SESSION['shopjpcart']=$str_Cart;return;}
        
        require_once( "../system/Class_M_DAO.php" );$M_dao = new Class_M_DAO;
        $SQL="SELECT count(*) as cnt FROM `d_cart` WHERE `user_id`=".$Userid;
        $data = $M_dao->get_rows($SQL);
        if($data[0]["cnt"]>0)
        {
            $SQL="UPDATE `d_cart` SET `cartValue`='".$str_Cart."' WHERE `user_id`=".$Userid;
        }
        else
        {
            $SQL="INSERT INTO `d_cart`(`user_id`, `cartValue`) VALUES (".$Userid.",'".$str_Cart."')";
        }
		$M_dao->exec_sql($SQL);
    }
   
    public static function clearCart($Userid)
    {
        if($Userid=="")
        {unset($_SESSION['shopjpcart']);return;}
        
        require_once( "../system/Class_M_DAO.php" );$M_dao = new Class_M_DAO;
        $SQL="DELETE FROM `d_cart` WHERE `user_id`=".$Userid;
        $M_dao->exec_sql($SQL);
    }

    public static function clearOtherTemp()
    {
        unset($_SESSION['shopjpgoodslist']);
		unset($_SESSION['shopjpextraprice']);
		unset($_SESSION['shopjpsendinfo']);
		unset($_SESSION['shopjpcardinfo']);
		unset($_SESSION['shopjpaddressinfo']);
		//unset($_SESSION['shopjpcart']);
		unset($_SESSION['shopjpRegisteraddressinfo']);
    }
}
?>